package com.loiane.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loiane.dataaccess.CheckDpropServerDatabaseAccessor;
import com.loiane.dataaccess.CheckURLDatabaseAccessor;
import com.loiane.model.DproperServerItem;
import com.loiane.model.URLCheckingResult;
import com.loiane.service.CheckURLService;
@Controller
public class CheckDpropServerConfigController {

	@RequestMapping("/dpropservercreate.do")
	public @ResponseBody String dpropserverCreate(){
		System.err.println("invoke dprop server create......");
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("success", true);
		JSONObject jso = JSONObject.fromObject(map);
		return jso.toString();		
	}
	
	@RequestMapping("/dpropserverupdate.do")	
	public @ResponseBody String dpropserverUpdate(@RequestBody String names){
		System.err.println("invoke dprop server update......");
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("success", true);
		JSONObject jso = JSONObject.fromObject(map);
//		Object obj = JSONObject.fromObject(names).get("names");
//		TransformerItem ti = null;
		
//		if(obj instanceof JSONArray){
//			@SuppressWarnings("unchecked")
//			Iterator<JSONObject> it = ((JSONArray) obj).iterator();
//			while(it.hasNext()){
//				JSONObject js = it.next();
//				ti = new TransformerItem();
//				ti.setId(Integer.valueOf(js.get("id").toString()));
//				ti.setName(js.get("name").toString());
//			}
//		}else if(obj instanceof JSONObject){
//			
//		}
		return names;
	}
	
	@RequestMapping("/dpropserverdestroy.do")
	public @ResponseBody String dpropserverDestroy(){
		System.err.println("invoke dprop server destroy......");
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("success", true);
		JSONObject jso = JSONObject.fromObject(map);
		return jso.toString();		
	}
	
	@RequestMapping("/dpropserverread.do")
	public @ResponseBody String dpropserverRead(){
		System.err.println("invoke dprop server read......");
		Map<String,Object> map = new HashMap<String,Object>();
		CheckDpropServerDatabaseAccessor cda = new CheckDpropServerDatabaseAccessor();
		List<DproperServerItem> dpropservers = cda.listAllDpropServers();
		map.put("success", true);
		map.put("servers", dpropservers);
		JSONObject jso = JSONObject.fromObject(map);
		return jso.toString();		
	}
	
	@RequestMapping("/checkDprop.do")
	public @ResponseBody String checkDpropServer(){
		System.err.println("invoke check dprop server......");
		Map<String,Object> map = new HashMap<String,Object>();	
		List<URLCheckingResult> result = new ArrayList<URLCheckingResult>();
		CheckURLService cs = new CheckURLDatabaseAccessor();
//		try {
//			result = cs.checkURLs();
			URLCheckingResult ur1 = new URLCheckingResult();
			ur1.setAvailable(false);
			ur1.setUrl("http://163.com");
			result.add(ur1);
			
			URLCheckingResult ur2 = new URLCheckingResult();
			ur2.setAvailable(true);
			ur2.setUrl("http://localhost:8080/producthealthtool");
			result.add(ur2);
			
			map.put("success", true);			
			map.put("checking", result);
//		} catch (CheckURLException e) {
//			e.printStackTrace();
//			map.put("success", false)	;	
//		}
		JSONObject jso = JSONObject.fromObject(map);
		return jso.toString();
	}
}
